import { request } from "../../request/index"

// pages/good-detail/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodInfo: {},
        addCartGoodList: [],
        collectChecked: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getGoodDetail(options),
            this.setData({
                addCartGoodList: wx.getStorageSync("cartGoodList") ? wx.getStorageSync("cartGoodList") : []
            })
        console.log(this.data.addCartGoodList);
    },
    getGoodDetail(data) {
        request({ url: "/goods/detail", data }).then(res => {
            this.setData({
                goodInfo: res.data.message
            })
        })
    },
    imagePreview(e) {
        wx.previewImage({
            current: e.currentTarget.dataset.id.pics_sma_url, // 当前显示图片的 http 链接
            urls: this.data.goodInfo.pics.map(v => {
                return v.pics_sma_url
            }) // 需要预览的图片 http 链接列表
        })
    },
    addCart(e) {
        let goodItem = {
            goods_id: e.target.dataset.good.goods_id,
            goods_price: e.target.dataset.good.goods_price,
            goods_name: e.target.dataset.good.goods_name,
            goods_small_logo: e.target.dataset.good.goods_small_logo,
            num: 1,
            checked: true
        }
        console.log(this.data.addCartGoodList.findIndex(v => v.goods_id == goodItem.goods_id));
        if (this.data.addCartGoodList.findIndex(v => v.goods_id == goodItem.goods_id) == -1) {
            this.data.addCartGoodList.push(goodItem)
        } else {
            let index = this.data.addCartGoodList.findIndex(v => v.goods_id = goodItem.goods_id)
            this.data.addCartGoodList[index].num++
        }
        wx.setStorageSync("cartGoodList", this.data.addCartGoodList);
        wx.showToast({
            title: '已加入购物车',
            icon: 'none',
            image: '',
            duration: 1500,
            mask: true,
            success: (result) => {

            },
            fail: () => { },
            complete: () => { }
        });
    },
    buy(e) {
        let paygood = {}
        paygood.goods_id = e.target.dataset.good.goods_id,
            paygood.goods_name = e.target.dataset.good.goods_name,
            paygood.goods_price = e.target.dataset.good.goods_price,
            paygood.goods_small_logo = e.target.dataset.good.goods_small_logo,
            paygood.num = 1
        paygood.checked = true
        let a = []
        a.push(paygood)
        wx.setStorageSync("payGood", a)
        wx.navigateTo({
            url: "/pages/pay/index",
        });

    },
    collectClick(e) {
        let goodItem = wx.getStorageSync("collectList") || []
        this.setData({
            collectChecked: !this.data.collectChecked
        })
        let paygood = {}
        paygood.goods_id = e.target.dataset.id.goods_id,
            paygood.goods_name = e.target.dataset.id.goods_name,
            paygood.goods_price = e.target.dataset.id.goods_price,
            paygood.goods_small_logo = e.target.dataset.id.goods_small_logo,
            paygood.num = 1
        let dd = goodItem.findIndex(v => v.goods_id == e.target.dataset.id.goods_id)
        if (this.data.collectChecked == true) {
            if (goodItem.length == 0 || dd == -1) {
                goodItem.push(paygood)
                wx.showToast({
                    title: '收藏商品成功！',
                    icon: 'success',
                })
            }
        } else {
            goodItem.splice(dd, 1)
        }
        wx.setStorageSync("collectList", goodItem)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let page = getCurrentPages();
        if (wx.getStorageSync("collectList").findIndex(v => v.goods_id == page[page.length - 1].options.goods_id) != -1) {
            this.setData({
                collectChecked: true
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})